package com.wanchuang.qhbb.module.shop;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.entity.node.BaseNode;
import com.wanchuang.qhbb.R;
import com.wanchuang.qhbb.base.BaseActivity;
import com.wanchuang.qhbb.net.Apis;
import com.wanchuang.qhbb.net.NetworkManager;
import com.wanchuang.qhbb.net.interfaces.OnRequestCallBack;
import com.wanchuang.qhbb.proto.Mystock;
import com.google.protobuf.InvalidProtocolBufferException;
import com.wanchuang.qhbb.utils.StatusBarUtil;
import com.wanchuang.qhbb.view.shop.FirstNode;
import com.wanchuang.qhbb.view.shop.SecondNode;
import com.wanchuang.qhbb.view.shop.NodeSectionAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class ShopActivity extends BaseActivity {

    @BindView(R.id.back_iv)
    ImageView backIv;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;

    private NodeSectionAdapter mAdapter;

    @BindView(R.id.tv_total)
    TextView tv_total;
    @BindView(R.id.tv_zhi_jie)
    TextView tv_zhi_jie;
    @BindView(R.id.tv_jian_jie)
    TextView tv_jian_jie;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shop);
        StatusBarUtil.setWhiteStatusBar(this);
        ButterKnife.bind(this);
        tvTitle.setText("我的商户");
        initRecyclerView();
        initListener();
        loadData();
    }

    private void initListener() {
        backIv.setOnClickListener(view -> finish());
    }

    private void loadData() {
        NetworkManager.INSTANCE.post(Apis.goods_merchant, new OnRequestCallBack() {
            @Override
            public void onOk(byte[] response) throws InvalidProtocolBufferException {
                Mystock.goods_merchant result = Mystock.goods_merchant.parseFrom(response);
                Log.e("wang", "onOk: " + result.toString());
                List<Mystock.list_basis> list = result.getListDesList();
                List<Mystock.goods_merchant.tab> tabs = result.getGoodsTabListList();
                StringBuilder des = new StringBuilder();
                for (int i = 0; i < list.size() - 1; i++) {
                    des.append(list.get(i).getName()).append("\n");
                }
                des.append(list.get(list.size() - 1).getName());

                tv_total.setText(result.getTotalNum());
                tv_zhi_jie.setText(result.getDirMerchantNum());
                tv_jian_jie.setText(result.getIndirMerchantNum());

                //总的 list，里面放的是 FirstNode
                List<BaseNode> listNode = new ArrayList<>();
                for (int i = 0; i < tabs.size(); i++) {
                    //SecondNode 的 list
                    List<BaseNode> secondNodeList = new ArrayList<>();
                    // 切割
                    List<Mystock.goods> splits = tabs.get(i).getListList();
                    for (int n = 0; n < splits.size(); n++) {
                        SecondNode seNode = new SecondNode(splits.get(n));
                        seNode.setTitle(tabs.get(i).getGoodsTab());
                        secondNodeList.add(seNode);
                    }
                    FirstNode entity = new FirstNode(secondNodeList, tabs.get(i).getGoodsTab(), i);
                    listNode.add(entity);
                }
                mAdapter.addData(listNode);
            }

            @Override
            public void onError(int errorCode, String errorMessage) {
                mAdapter.setUseEmpty(true);
            }
        });
    }


    @Override
    protected void onResume() {
        super.onResume();

    }

    private void initRecyclerView() {
        recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        recyclerView.setAdapter(mAdapter = new NodeSectionAdapter());
        mAdapter.setEmptyView(R.layout.recycle_empty);
        mAdapter.setUseEmpty(true);
        // 每个item的点击事件
        mAdapter.addChildClickViewIds(R.id.rrl_root);
        mAdapter.setOnItemChildClickListener((adapter1, view, position) -> {
            SecondNode data = (SecondNode) view.getTag();
            Mystock.goods shopInfo = data.getGoodsInfo();
            String title = data.getTitle();
            Intent intent = new Intent(mContext, ShopNextActivity.class);
            intent.putExtra("title", title + " " + shopInfo.getGoodsName() + " " + shopInfo.getGoodsSerial());
            intent.setType(shopInfo.getGoodsCommonid());
            startActivity(intent);
        });
    }

}
